package com.yihu.cms.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.yihu.beans.ResultVO;
import com.yihu.beans.user.Group;
import com.yihu.beans.user.GroupQuery;
import com.yihu.beans.user.User;
import com.yihu.beans.user.UserGroupRef;
import com.yihu.cms.util.Osslog;
import com.yihu.dataService.hessian.mongodb.interfaceService.UserGroupHService;
import com.yihu.dataService.hessian.resource.ResourceMongoDBManager;

public class UserGroupService {

	UserGroupHService groupService = null;
	public UserGroupService(){
		try {
			groupService= (UserGroupHService)ResourceMongoDBManager.getSingleton().getBeans("userGroupServiceHSC");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ResultVO addGroup(Group group){
		return groupService.addGroup(group);
	}
	public ResultVO deleteGroup(long id){
		return groupService.deleteGroup(id);
	}
	public ResultVO updateGroup(Group group){
		return groupService.updateGroup(group);
	}
	public Group getGroup(long id){
		return groupService.getGroup(id);
	}
	public String getGroupByPage(GroupQuery query){
		return groupService.getGroupByPage(query);
	}
	
	public Group  getUsersByGroupId(GroupQuery query){
		return groupService.getUsersByGroupId(query);
	}
	
	public ResultVO addUserToGroup(UserGroupRef ugr){
		return groupService.addUserToGroup(ugr);
	}
	
	public ResultVO deleteUserFromGroup(UserGroupRef ugr){
		return groupService.deleteUserFromGroup(ugr);
	}
	
	public ResultVO assignUsers(UserGroupRef ugr){
		return groupService.assignUsers(ugr);
	}	
	public List<User> getUsersByGroup(GroupQuery query){
		return groupService.getUsersByGroup(query);
	}
	
	public List<String> getOpenidsByGroup(GroupQuery query){
		List<User> users = groupService.getUsersByGroup(query);
		List<String> openids = new ArrayList<String>();
		for(User u:users){
			if(StringUtils.isBlank(u.getOpenid()))
				continue;
			openids.add(u.getOpenid().trim());
		}
		Osslog.debug("getOpenidsByGroup.groupName: "+query.getGroupName()+" result:"+openids);
		return openids;
	}
	
}
